gl path "~/Dropbox/voter_id/Replication"

cap log close
log using "$path/table_a1.log", replace

use "$path/nc_dataset.dta" if new_reg == 0, clear

drop black white hispanic othernw new_reg noid_p_2016 dem rep

// drop 2018 
drop if inlist(election, 11, 12)

sort id election

replace voted = 0 if voted == .

// replace voted with NA if under 18 / ineligible in that election year 
drop if birth_year > 1998 & birth_year < 9999
replace voted = . if (birth_year > 1990 & birth_year < 9999) & (election == 1 | election == 2) // 2008
replace voted = . if (birth_year > 1992 & birth_year < 9999) & (election == 3 | election == 4) // 2010
replace voted = . if (birth_year > 1994 & birth_year < 9999) & (election == 5 | election == 6) // 2012
replace voted = . if (birth_year > 1996 & birth_year < 9999) & (election == 7 | election == 8) // 2014

** create string encoding of possible pre-treatment outcome paths
tostring voted, generate(outcome_path)

// get pre-treatment path thorugh 2012 general for placebo test for 2014 primary
by id: gen outcome_path_pretreat = outcome_path[1] + outcome_path[2] + ///
								outcome_path[3] + outcome_path[4] + ///
								outcome_path[5] + outcome_path[6] 


// keep primary only
keep if mod(election,2) == 1

replace voted = 0 if voted == .

gen count = 1

// generate lead
gen treat_lead = treat[_n+1] if id == id[_n+1]
replace treat_lead = 0 if treat_lead == .

// construct age bins, give young voters who are ineligible for some election their own age bin
gen age_bin = 11 if (birth_year > 1990 & birth_year < 9999)
replace age_bin = 12 if (birth_year > 1992 & birth_year < 9999)
replace age_bin = 13 if (birth_year > 1994 & birth_year < 9999)
replace age_bin = 14 if (birth_year > 1996 & birth_year < 9999)
// give those who are eligible over the whole period their own age decile
xtile age_decile = birth_year if (birth_year <= 1990), nq(10)
replace age_bin = age_decile if (birth_year <= 1990)
drop age_decile

compress

sort id election


* vanilla diff in diff
reghdfe voted treat treat_lead, a(id election) cluster(id)
local b1 = _b[treat]
local se1 = _se[treat]
local b1_2 = _b[treat_lead]
local se1_2 = _se[treat_lead]
local n1 = e(N)
local nclust1 = e(N_clust)

* race by year
reghdfe voted treat treat_lead, a(id race_by_year) cluster(id)
local b2 = _b[treat]
local se2 = _se[treat]
local b2_2 = _b[treat_lead]
local se2_2 = _se[treat_lead]
local n2 = e(N)
local nclust2 = e(N_clust)

* age by year 
reghdfe voted treat treat_lead, a(id age_by_year) cluster(id)
local b3 = _b[treat]
local se3 = _se[treat]
local b3_2 = _b[treat_lead]
local se3_2 = _se[treat_lead]
local n3 = e(N)
local nclust3 = e(N_clust)

* age by race by year
reghdfe voted treat treat_lead, a(id race_by_age_by_year) cluster(id)
local b4 = _b[treat]
local se4 = _se[treat]
local b4_2 = _b[treat_lead]
local se4_2 = _se[treat_lead]
local n4 = e(N)
local nclust4 = e(N_clust)


// drop 2016 primary
drop if election == 9 

preserve 


collapse (sum) count (mean) voted, by(outcome_path_pretreat no_dmv_match election)

*** get N by path by summing together n treated and n control
by outcome_path: gen tot_tmp = count[5] if _n==5
egen tot_treat = sum(tot_tmp), by(outcome_path)
by outcome_path: replace tot_tmp = count[1] if _n == 1
egen tot = sum(tot_tmp), by(outcome_path)
// N_voters is the number of voters who enter into the regression
egen N_voters = sum(tot_tmp)
// get number of elections
unique election
// N is number of voters * number of elections
gen long N = N_voters * r(unique)
drop tot_tmp
gen tot_control = tot - tot_treat

*** get weights for fw based on total n per stratum
*** each stratum has 8 obs
*** fw requires integers so need to round
gen tot2 = round(tot/8)
gen tot_treat2 = round(tot_treat/8)
egen op = group(outcome_path)

reghdfe voted no_dmv_match [fw=tot_treat2], a(op election)
local b5 = _b[no_dmv_match]
local se5 = _se[no_dmv_match]
local n5 = N
local nclust5 = N_voters

restore 
preserve 


collapse (sum) count (mean) voted, by(outcome_path_pretreat no_dmv_match election race_string)

*** get N by path by summing together n treated and n control
sort outcome_path race_string no_dmv_match election
by outcome_path race_string: gen tot_tmp = count[5] if _n==5
egen tot_treat = sum(tot_tmp), by(outcome_path race_string)
by outcome_path race_string: replace tot_tmp = count[1] if _n == 1
egen tot = sum(tot_tmp), by(outcome_path race_string)
// N_voters is the number of voters who enter into the regression
egen N_voters = sum(tot_tmp)
// get number of elections
unique election
// N is number of voters * number of elections
gen long N = N_voters * r(unique)
drop tot_tmp
gen tot_control = tot - tot_treat

*** get weights for fw based on total n per stratum
*** each stratum has 8 obs
*** fw requires integers so need to round
gen tot2 = round(tot/8)
gen tot_treat2 = round(tot_treat/8)
egen op = group(outcome_path race_string)

reghdfe voted no_dmv_match [fw=tot_treat2], a(op election)
local b6 = _b[no_dmv_match]
local se6 = _se[no_dmv_match]
local n6 = N
local nclust6 = N_voters

restore

collapse (sum) count (mean) voted, by(outcome_path_pretreat no_dmv_match election race_string age_bin)

// drop if age is missing
drop if age_bin == .

*** get N by path by summing together n treated and n control
sort outcome_path race_string age_bin no_dmv_match election
by outcome_path race_string age_bin: gen tot_tmp = count[5] if _n==5
egen tot_treat = sum(tot_tmp), by(outcome_path race_string age_bin)
by outcome_path race_string age_bin: replace tot_tmp = count[1] if _n == 1
egen tot = sum(tot_tmp), by(outcome_path race_string age_bin)
// N_voters is the number of voters who enter into the regression
egen N_voters = sum(tot_tmp)
// get number of elections
unique election
// N is number of voters * number of elections
gen long N = N_voters * r(unique)
drop tot_tmp
gen tot_control = tot - tot_treat

*** get weights for fw based on total n per stratum
*** each stratum has 8 obs
*** fw requires integers so need to round
gen tot2 = round(tot/8)
gen tot_treat2 = round(tot_treat/8)
egen op = group(outcome_path race_string age_bin)

reghdfe voted no_dmv_match [fw=tot_treat2], a(op election)
local b7 = _b[no_dmv_match]
local se7 = _se[no_dmv_match]
local n7 = N
local nclust7 = N_voters

log close


quietly {
	cap log close
	set linesize 255
	log using "$path/table_a1.tex", text replace
	
	noisily dis "\begin{table}[h]"
	noisily dis "\centering"
	noisily dis "\caption{\textbf{Evidence of Pre-Trending: Effect of Voter ID Law Lead on Primary Election Turnout Among Those Without ID, Individual Level, 2008--2016.}"
	noisily dis "\label{tab:voter_id_primary_pre_trending}}"
	noisily dis "\resizebox{\textwidth}{!} {"
	noisily dis "\begin{tabular}{lccccccc}"
	noisily dis "\toprule \toprule"
	noisily dis " & \multicolumn{7}{c}{Voted in Primary (0-1)}\\"
	noisily dis " & (1) & (2) & (3) & (4) & (5) & (6) & (7)  \\"
	noisily dis "\midrule"
	
	noisily dis "No DMV Match * Year $=$ 2016 & " %4.3f `b1' " & " %4.3f `b2' " & " %4.3f `b3' " & " %4.3f `b4' " & &  &  \\"
	noisily dis " & (" %4.3f `se1' ") & (" %4.3f `se2' ") & (" %4.3f `se3' ") & (" %4.3f `se4' ") &  &  &  \smallskip\\"
	
	noisily dis "No DMV Match * Year $=$ 2014 & " %4.3f `b1_2' " & " %4.3f `b2_2' " & " %4.3f `b3_2' " & " %4.3f `b4_2' " & " %4.3f `b5' " & " %4.3f `b6' " & " %4.3f `b7'  "\\"
	noisily dis " & (" %4.3f `se1_2' ") & (" %4.3f `se2_2' ") & (" %4.3f `se3_2' ") & (" %4.3f `se4_2' ") & (" %4.3f `se5' ") & (" %4.3f `se6' ")  & (" %4.3f `se7' ")  \smallskip\\"
	
	noisily dis " N & " %12.0fc `n1' " & " %12.0fc `n2' " & " %12.0fc `n3' " & " %12.0fc `n4' " & " %12.0fc `n5' " & " %12.0fc `n6' " & " %12.0fc `n7' " \\ "
	noisily dis " \# Voters & "  %12.0fc `nclust1' " & " %12.0fc `nclust2' " & " %12.0fc `nclust3' " & " %12.0fc `nclust4' " & " %12.0fc `nclust5' " & " %12.0fc `nclust6' " & " %12.0fc `nclust7' " \\ "
	
	noisily dis "Individual FEs & Y & Y & Y & Y & N & N & N  \\"
	noisily dis "Year FEs & Yes & N & N & N & Y & Y & Y  \\"
	noisily dis "Race by Year FEs & N & Y & N & N & N & N & N  \\"
	noisily dis "Age by Year FEs & N & N & Y & N & N & N & N \\"
	noisily dis "Race by Age by Year FEs & N & N & N & Y & N & N & N \\"
	noisily dis "Exact Match on Turnout & N & N & N & N & Y & Y & Y \\"
	noisily dis "Exact Match on Race & N & N & N & N & N & Y & Y \\"
	noisily dis "Exact Match on Age Bin & N & N & N & N & N & N & Y \\"

	noisily dis "\bottomrule \bottomrule"
	noisily dis "\multicolumn{8}{p{1.15\textwidth}}{\footnotesize Robust standard errors clustered by individual in parentheses.  Main effect for No DMV Match is absorbed by fixed effects.  "
	noisily dis "Exact matching on turnout matches units based on all primary and general elections from 2008-2012.  For exact matching on age, we construct a separate age bin for "
	noisily dis "each group of voters who were under 18 for a given set of elections, so the cohort of voters who became newly eligible to participate in 2010, 2012, and 2014 each have their own age bin.  "
	noisily dis "For voters who were eligible for all elections since 2008, we construct age deciles.}"
	noisily dis "\end{tabular}}"
	noisily dis "\end{table}"
	
	log off
	
}




